********************************************************************************
**  TITLE:      on2011_ipsos_contact_networks                                 **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       2011 Ontario General Election Exit Survey                     **
**  DATE:	    October 2022 						                          **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "on2011_ipsos_contact_networks", replace

* Open the dataset

import spss using "2011 Ontario General Election Exit Survey.sav", clear case(lower)


***********************
** DISTRICT OUTCOMES **
***********************

decode riding, generate(riding_str)
gen str riding_code = substr(riding_str,1,5)
replace riding_code = "" if riding_str == "Not specified"
destring riding_code, gen(district_code)

* Election outcome in each district (1 = OPC, 2 = OLP, 3 = ONDP, 4 = GPO, 5 = other)

gen district_outcome = .

replace district_outcome = 2 if district_code == 3501
replace district_outcome = 3 if district_code == 3502
replace district_outcome = 2 if district_code == 3503
replace district_outcome = 1 if district_code == 3504
replace district_outcome = 3 if district_code == 3505
replace district_outcome = 3 if district_code == 3506
replace district_outcome = 2 if district_code == 3507
replace district_outcome = 2 if district_code == 3508
replace district_outcome = 2 if district_code == 3509
replace district_outcome = 1 if district_code == 35010
replace district_outcome = 1 if district_code == 35011
replace district_outcome = 1 if district_code == 35012
replace district_outcome = 1 if district_code == 35013
replace district_outcome = 1 if district_code == 35014
replace district_outcome = 3 if district_code == 35015
replace district_outcome = 2 if district_code == 35016
replace district_outcome = 2 if district_code == 35017
replace district_outcome = 1 if district_code == 35018
replace district_outcome = 1 if district_code == 35019
replace district_outcome = 2 if district_code == 35020
replace district_outcome = 1 if district_code == 35021
replace district_outcome = 3 if district_code == 35022
replace district_outcome = 2 if district_code == 35023
replace district_outcome = 2 if district_code == 35024
replace district_outcome = 2 if district_code == 35025
replace district_outcome = 2 if district_code == 35026
replace district_outcome = 2 if district_code == 35027
replace district_outcome = 1 if district_code == 35028
replace district_outcome = 1 if district_code == 35029
replace district_outcome = 1 if district_code == 35030
replace district_outcome = 3 if district_code == 35031
replace district_outcome = 3 if district_code == 35032
replace district_outcome = 3 if district_code == 35033
replace district_outcome = 1 if district_code == 35034
replace district_outcome = 3 if district_code == 35035
replace district_outcome = 2 if district_code == 35036
replace district_outcome = 2 if district_code == 35037
replace district_outcome = 1 if district_code == 35038
replace district_outcome = 1 if district_code == 35039
replace district_outcome = 1 if district_code == 35040
replace district_outcome = 1 if district_code == 35041
replace district_outcome = 1 if district_code == 35042
replace district_outcome = 3 if district_code == 35043
replace district_outcome = 2 if district_code == 35044
replace district_outcome = 2 if district_code == 35045
replace district_outcome = 2 if district_code == 35046
replace district_outcome = 2 if district_code == 35047
replace district_outcome = 2 if district_code == 35048
replace district_outcome = 2 if district_code == 35049
replace district_outcome = 2 if district_code == 35050
replace district_outcome = 2 if district_code == 35051
replace district_outcome = 1 if district_code == 35052
replace district_outcome = 1 if district_code == 35053
replace district_outcome = 2 if district_code == 35054
replace district_outcome = 1 if district_code == 35055
replace district_outcome = 3 if district_code == 35056
replace district_outcome = 1 if district_code == 35057
replace district_outcome = 1 if district_code == 35058
replace district_outcome = 2 if district_code == 35059
replace district_outcome = 2 if district_code == 35060
replace district_outcome = 1 if district_code == 35061
replace district_outcome = 2 if district_code == 35062
replace district_outcome = 2 if district_code == 35063
replace district_outcome = 2 if district_code == 35064
replace district_outcome = 2 if district_code == 35065
replace district_outcome = 2 if district_code == 35066
replace district_outcome = 1 if district_code == 35067
replace district_outcome = 3 if district_code == 35068
replace district_outcome = 1 if district_code == 35069
replace district_outcome = 1 if district_code == 35070
replace district_outcome = 2 if district_code == 35071
replace district_outcome = 2 if district_code == 35072
replace district_outcome = 1 if district_code == 35073
replace district_outcome = 1 if district_code == 35074
replace district_outcome = 2 if district_code == 35075
replace district_outcome = 2 if district_code == 35076
replace district_outcome = 2 if district_code == 35077
replace district_outcome = 1 if district_code == 35078
replace district_outcome = 2 if district_code == 35079
replace district_outcome = 2 if district_code == 35080
replace district_outcome = 2 if district_code == 35081
replace district_outcome = 2 if district_code == 35082
replace district_outcome = 2 if district_code == 35083
replace district_outcome = 2 if district_code == 35084
replace district_outcome = 1 if district_code == 35085
replace district_outcome = 1 if district_code == 35086
replace district_outcome = 1 if district_code == 35087
replace district_outcome = 2 if district_code == 35088
replace district_outcome = 1 if district_code == 35089
replace district_outcome = 2 if district_code == 35090
replace district_outcome = 2 if district_code == 35091
replace district_outcome = 3 if district_code == 35092
replace district_outcome = 3 if district_code == 35093
replace district_outcome = 2 if district_code == 35094
replace district_outcome = 3 if district_code == 35095
replace district_outcome = 3 if district_code == 35096
replace district_outcome = 2 if district_code == 35097
replace district_outcome = 3 if district_code == 35098
replace district_outcome = 1 if district_code == 35099
replace district_outcome = 1 if district_code == 35100
replace district_outcome = 2 if district_code == 35101
replace district_outcome = 2 if district_code == 35102
replace district_outcome = 2 if district_code == 35103
replace district_outcome = 2 if district_code == 35104
replace district_outcome = 1 if district_code == 35105
replace district_outcome = 2 if district_code == 35106
replace district_outcome = 2 if district_code == 35107


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 11.8016321406152 if district_code == 3501
replace margin = 16.0928373808792 if district_code == 3502
replace margin = 9.1153139696829 if district_code == 3503
replace margin = 5.84783112966829 if district_code == 3504
replace margin = 10.7404988899047 if district_code == 3505
replace margin = 5.22583310382814 if district_code == 3506
replace margin = 8.25084380406728 if district_code == 3507
replace margin = 10.9037104484407 if district_code == 3508
replace margin = 2.43162430744878 if district_code == 3509
replace margin = 21.0090543746671 if district_code == 35010
replace margin = 4.33486423334139 if district_code == 35011
replace margin = 4.61829354762468 if district_code == 35012
replace margin = 16.2052450727295 if district_code == 35013
replace margin = 9.64248217936675 if district_code == 35014
replace margin = 4.5206048786726 if district_code == 35015
replace margin = 23.8831615120275 if district_code == 35016
replace margin = 27.7142925304769 if district_code == 35017
replace margin = 20.1948137422667 if district_code == 35018
replace margin = 19.7212421380202 if district_code == 35019
replace margin = 20.6006679887277 if district_code == 35020
replace margin = 21.0500835128658 if district_code == 35021
replace margin = 2.98403280691039 if district_code == 35022
replace margin = 18.6990533016984 if district_code == 35023
replace margin = 21.782360522924 if district_code == 35024
replace margin = 24.1025229633789 if district_code == 35025
replace margin = 3.41531414915862 if district_code == 35026
replace margin = 16.8340578623895 if district_code == 35027
replace margin = 41.3892105771087 if district_code == 35028
replace margin = 11.8500752063092 if district_code == 35029
replace margin = 5.33731201573388 if district_code == 35030
replace margin = 43.8674889027914 if district_code == 35031
replace margin = 25.4130088294077 if district_code == 35032
replace margin = 12.7785246732638 if district_code == 35033
replace margin = 10.008043973723 if district_code == 35034
replace margin = 11.9737140267392 if district_code == 35035
replace margin = 25.0563285405682 if district_code == 35036
replace margin = 0.823181609664103 if district_code == 35037
replace margin = 8.68339096103387 if district_code == 35038
replace margin = 7.73348956544577 if district_code == 35039
replace margin = 16.4165014632304 if district_code == 35040
replace margin = 22.2452851244281 if district_code == 35041
replace margin = 46.1717544272673 if district_code == 35042
replace margin = 12.490869247626 if district_code == 35043
replace margin = 14.9798405571337 if district_code == 35044
replace margin = 16.1675921251893 if district_code == 35045
replace margin = 21.1053522034536 if district_code == 35046
replace margin = 15.6382978723404 if district_code == 35047
replace margin = 12.4782851927097 if district_code == 35048
replace margin = 9.32892228819315 if district_code == 35049
replace margin = 14.6245551158565 if district_code == 35050
replace margin = 22.0022734203887 if district_code == 35051
replace margin = 27.5105837890873 if district_code == 35052
replace margin = 11.6231890449624 if district_code == 35053
replace margin = 1.06428449307574 if district_code == 35054
replace margin = 24.9683065472539 if district_code == 35055
replace margin = 30.6384500357584 if district_code == 35056
replace margin = 21.5221215872809 if district_code == 35057
replace margin = 1.45956770370982 if district_code == 35058
replace margin = 7.64196879942934 if district_code == 35059
replace margin = 10.1448633322996 if district_code == 35060
replace margin = 6.07232203775301 if district_code == 35061
replace margin = 17.6812971431965 if district_code == 35062
replace margin = 6.06434065700565 if district_code == 35063
replace margin = 15.4271143221419 if district_code == 35064
replace margin = 28.06879348825 if district_code == 35065
replace margin = 2.27088584803745 if district_code == 35066
replace margin = 29.876753081173 if district_code == 35067
replace margin = 8.77440128798551 if district_code == 35068
replace margin = 35.878436725257 if district_code == 35069
replace margin = 0.567184334908843 if district_code == 35070
replace margin = 8.44054420650253 if district_code == 35071
replace margin = 13.2492437060965 if district_code == 35072
replace margin = 7.03323371458104 if district_code == 35073
replace margin = 54.7979992304733 if district_code == 35074
replace margin = 11.1275810058772 if district_code == 35075
replace margin = 3.99344185501815 if district_code == 35076
replace margin = 37.4758141594983 if district_code == 35077
replace margin = 22.8716049382716 if district_code == 35078
replace margin = 24.1249914716518 if district_code == 35079
replace margin = 14.7252954488308 if district_code == 35080
replace margin = 27.4977698483497 if district_code == 35081
replace margin = 20.2859472001003 if district_code == 35082
replace margin = 5.9054685353119 if district_code == 35083
replace margin = 12.6402031623182 if district_code == 35084
replace margin = 32.0225562297648 if district_code == 35085
replace margin = 32.7468660655377 if district_code == 35086
replace margin = 33.595922150139 if district_code == 35087
replace margin = 1.6380294290033 if district_code == 35088
replace margin = 5.78670705630791 if district_code == 35089
replace margin = 1.65407854984894 if district_code == 35090
replace margin = 10.1522454288119 if district_code == 35091
replace margin = 24.2055147788137 if district_code == 35092
replace margin = 12.7731092436975 if district_code == 35093
replace margin = 29.7479101358412 if district_code == 35094
replace margin = 23.4009906320663 if district_code == 35095
replace margin = 2.4282607768729 if district_code == 35096
replace margin = 21.7837826888407 if district_code == 35097
replace margin = 12.5311620886032 if district_code == 35098
replace margin = 28.7875201212006 if district_code == 35099
replace margin = 14.7674098541151 if district_code == 35100
replace margin = 17.2436920372812 if district_code == 35101
replace margin = 9.98630173779915 if district_code == 35102
replace margin = 10.4772208901105 if district_code == 35103
replace margin = 9.84102484951381 if district_code == 35104
replace margin = 28.2424994185596 if district_code == 35105
replace margin = 2.36873527608352 if district_code == 35106
replace margin = 15.6646685472496 if district_code == 35107

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.69199630455734 if district_code == 3501
replace enep = 2.98144258189552 if district_code == 3502
replace enep = 2.93209676608835 if district_code == 3503
replace enep = 3.08246014669991 if district_code == 3504
replace enep = 2.70622260207851 if district_code == 3505
replace enep = 3.25998985592759 if district_code == 3506
replace enep = 2.83924372316572 if district_code == 3507
replace enep = 2.9707389574939 if district_code == 3508
replace enep = 3.15783587704476 if district_code == 3509
replace enep = 3.11089371583531 if district_code == 35010
replace enep = 3.03205234684088 if district_code == 35011
replace enep = 3.1958357134846 if district_code == 35012
replace enep = 2.61008808846147 if district_code == 35013
replace enep = 3.00603583698816 if district_code == 35014
replace enep = 2.56671360995013 if district_code == 35015
replace enep = 2.70297762987611 if district_code == 35016
replace enep = 2.26509299372499 if district_code == 35017
replace enep = 3.0720315903232 if district_code == 35018
replace enep = 2.78748028281907 if district_code == 35019
replace enep = 2.40582220879416 if district_code == 35020
replace enep = 2.84789621238481 if district_code == 35021
replace enep = 3.02692350313595 if district_code == 35022
replace enep = 2.58127720023594 if district_code == 35023
replace enep = 2.69906154698121 if district_code == 35024
replace enep = 2.92057676492443 if district_code == 35025
replace enep = 2.73813718923452 if district_code == 35026
replace enep = 3.25252521403078 if district_code == 35027
replace enep = 2.28753506179442 if district_code == 35028
replace enep = 2.8555504306369 if district_code == 35029
replace enep = 2.71234970767997 if district_code == 35030
replace enep = 2.34834874635374 if district_code == 35031
replace enep = 2.68786793725948 if district_code == 35032
replace enep = 2.89416988881858 if district_code == 35033
replace enep = 2.99280213431151 if district_code == 35034
replace enep = 2.51044806509614 if district_code == 35035
replace enep = 2.88652900289002 if district_code == 35036
replace enep = 2.96372254360398 if district_code == 35037
replace enep = 2.8340799803796 if district_code == 35038
replace enep = 2.85758840469462 if district_code == 35039
replace enep = 2.94358861633346 if district_code == 35040
replace enep = 2.75349796077605 if district_code == 35041
replace enep = 2.17733329643899 if district_code == 35042
replace enep = 3.15222631088525 if district_code == 35043
replace enep = 3.03743822450185 if district_code == 35044
replace enep = 2.91404134753003 if district_code == 35045
replace enep = 2.55399356193831 if district_code == 35046
replace enep = 3.01240619994221 if district_code == 35047
replace enep = 2.86615090370313 if district_code == 35048
replace enep = 2.78149577218964 if district_code == 35049
replace enep = 2.51246125904727 if district_code == 35050
replace enep = 2.64907477151359 if district_code == 35051
replace enep = 2.54911744635811 if district_code == 35052
replace enep = 2.69182348047739 if district_code == 35053
replace enep = 3.12930728319955 if district_code == 35054
replace enep = 2.7594533223847 if district_code == 35055
replace enep = 2.53963057946301 if district_code == 35056
replace enep = 2.7267555778309 if district_code == 35057
replace enep = 2.95984825019955 if district_code == 35058
replace enep = 2.80138769398199 if district_code == 35059
replace enep = 2.58880946702329 if district_code == 35060
replace enep = 2.93452133993124 if district_code == 35061
replace enep = 2.94510680068964 if district_code == 35062
replace enep = 2.56205394926933 if district_code == 35063
replace enep = 2.70652068678024 if district_code == 35064
replace enep = 2.77096392233681 if district_code == 35065
replace enep = 2.84789299114732 if district_code == 35066
replace enep = 2.56840966078515 if district_code == 35067
replace enep = 2.71422763304873 if district_code == 35068
replace enep = 2.72488806696937 if district_code == 35069
replace enep = 2.91745519874861 if district_code == 35070
replace enep = 3.07832824616264 if district_code == 35071
replace enep = 2.79852423754817 if district_code == 35072
replace enep = 3.00597471540278 if district_code == 35073
replace enep = 1.85577827096172 if district_code == 35074
replace enep = 2.73042795013483 if district_code == 35075
replace enep = 2.99187063258595 if district_code == 35076
replace enep = 2.42069879028441 if district_code == 35077
replace enep = 2.88521956467237 if district_code == 35078
replace enep = 2.43048135744509 if district_code == 35079
replace enep = 2.8685577934544 if district_code == 35080
replace enep = 2.70170803688578 if district_code == 35081
replace enep = 2.78095388910417 if district_code == 35082
replace enep = 2.93834737769571 if district_code == 35083
replace enep = 2.9451390955065 if district_code == 35084
replace enep = 2.67374459019225 if district_code == 35085
replace enep = 2.58910265709675 if district_code == 35086
replace enep = 2.53063694954672 if district_code == 35087
replace enep = 2.74275088649803 if district_code == 35088
replace enep = 2.5369274820973 if district_code == 35089
replace enep = 2.94517352534672 if district_code == 35090
replace enep = 2.81573075324203 if district_code == 35091
replace enep = 2.75025923131251 if district_code == 35092
replace enep = 2.53308578303412 if district_code == 35093
replace enep = 2.56120103591993 if district_code == 35094
replace enep = 2.52751413068295 if district_code == 35095
replace enep = 2.81736614998369 if district_code == 35096
replace enep = 2.55034403603901 if district_code == 35097
replace enep = 2.92072228632495 if district_code == 35098
replace enep = 2.48037071243946 if district_code == 35099
replace enep = 2.71732345137734 if district_code == 35100
replace enep = 2.57493726440802 if district_code == 35101
replace enep = 2.98229366399441 if district_code == 35102
replace enep = 3.04800027726171 if district_code == 35103
replace enep = 2.83402608543692 if district_code == 35104
replace enep = 2.70556099402188 if district_code == 35105
replace enep = 2.57066988034658 if district_code == 35106
replace enep = 2.55789608021229 if district_code == 35107


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if district_code == 3501
replace reelected = 0 if district_code == 3502
replace reelected = 1 if district_code == 3503
replace reelected = 0 if district_code == 3504
replace reelected = 1 if district_code == 3505
replace reelected = 0 if district_code == 3506
replace reelected = 1 if district_code == 3507
replace reelected = 1 if district_code == 3508
replace reelected = 1 if district_code == 3509
replace reelected = 1 if district_code == 35010
replace reelected = 1 if district_code == 35011
replace reelected = 1 if district_code == 35012
replace reelected = 1 if district_code == 35013
replace reelected = 0 if district_code == 35014
replace reelected = 0 if district_code == 35015
replace reelected = 1 if district_code == 35016
replace reelected = 1 if district_code == 35017
replace reelected = 1 if district_code == 35018
replace reelected = 1 if district_code == 35019
replace reelected = 1 if district_code == 35020
replace reelected = 0 if district_code == 35021
replace reelected = 0 if district_code == 35022
replace reelected = 1 if district_code == 35023
replace reelected = 1 if district_code == 35024
replace reelected = 1 if district_code == 35025
replace reelected = 1 if district_code == 35026
replace reelected = 1 if district_code == 35027
replace reelected = 1 if district_code == 35028
replace reelected = 1 if district_code == 35029
replace reelected = 1 if district_code == 35030
replace reelected = 1 if district_code == 35031
replace reelected = 1 if district_code == 35032
replace reelected = 0 if district_code == 35033
replace reelected = 0 if district_code == 35034
replace reelected = 1 if district_code == 35035
replace reelected = 1 if district_code == 35036
replace reelected = 1 if district_code == 35037
replace reelected = 0 if district_code == 35038
replace reelected = 1 if district_code == 35039
replace reelected = 0 if district_code == 35040
replace reelected = 1 if district_code == 35041
replace reelected = 1 if district_code == 35042
replace reelected = 0 if district_code == 35043
replace reelected = 1 if district_code == 35044
replace reelected = 1 if district_code == 35045
replace reelected = 1 if district_code == 35046
replace reelected = 1 if district_code == 35047
replace reelected = 1 if district_code == 35048
replace reelected = 1 if district_code == 35049
replace reelected = 1 if district_code == 35050
replace reelected = 1 if district_code == 35051
replace reelected = 1 if district_code == 35052
replace reelected = 1 if district_code == 35053
replace reelected = 1 if district_code == 35054
replace reelected = 1 if district_code == 35055
replace reelected = 1 if district_code == 35056
replace reelected = 0 if district_code == 35057
replace reelected = 0 if district_code == 35058
replace reelected = 1 if district_code == 35059
replace reelected = 1 if district_code == 35060
replace reelected = 1 if district_code == 35061
replace reelected = 1 if district_code == 35062
replace reelected = 1 if district_code == 35063
replace reelected = 1 if district_code == 35064
replace reelected = 1 if district_code == 35065
replace reelected = 1 if district_code == 35066
replace reelected = 1 if district_code == 35067
replace reelected = 1 if district_code == 35068
replace reelected = 1 if district_code == 35069
replace reelected = 0 if district_code == 35070
replace reelected = 1 if district_code == 35071
replace reelected = 1 if district_code == 35072
replace reelected = 0 if district_code == 35073
replace reelected = 1 if district_code == 35074
replace reelected = 1 if district_code == 35075
replace reelected = 1 if district_code == 35076
replace reelected = 1 if district_code == 35077
replace reelected = 1 if district_code == 35078
replace reelected = 1 if district_code == 35079
replace reelected = 1 if district_code == 35080
replace reelected = 1 if district_code == 35081
replace reelected = 1 if district_code == 35082
replace reelected = 1 if district_code == 35083
replace reelected = 1 if district_code == 35084
replace reelected = 1 if district_code == 35085
replace reelected = 1 if district_code == 35086
replace reelected = 0 if district_code == 35087
replace reelected = 1 if district_code == 35088
replace reelected = 1 if district_code == 35089
replace reelected = 1 if district_code == 35090
replace reelected = 1 if district_code == 35091
replace reelected = 0 if district_code == 35092
replace reelected = 1 if district_code == 35093
replace reelected = 1 if district_code == 35094
replace reelected = 1 if district_code == 35095
replace reelected = 1 if district_code == 35096
replace reelected = 1 if district_code == 35097
replace reelected = 1 if district_code == 35098
replace reelected = 1 if district_code == 35099
replace reelected = 1 if district_code == 35100
replace reelected = 1 if district_code == 35101
replace reelected = 1 if district_code == 35102
replace reelected = 1 if district_code == 35103
replace reelected = 1 if district_code == 35104
replace reelected = 1 if district_code == 35105
replace reelected = 1 if district_code == 35106
replace reelected = 1 if district_code == 35107


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen time = 0

* Gender

gen male = pmgender

recode male (2=0)

* Age

gen age = pmage

* Education

gen education = pmed

recode education (9=.)

* Vote status (provincial)

gen vote_whole = 1 if vote == 2
replace vote_whole = 0 if vote!=2 & vote!=. 

* Vote status (district)

gen vote_district = 1 if vote == district_outcome
replace vote_district = 0 if vote!=district_outcome & vote!=. 

* Political discussion

gen discussion = q28

recode discussion (5=.)
recode discussion (6=.)

replace discussion = 4 - discussion

gen discussion_3pts = 0 if discussion == 0
replace discussion_3pts = 0 if discussion == 1
replace discussion_3pts = 1 if discussion == 2
replace discussion_3pts = 2 if discussion == 3


***********************************
** PROVINCIAL-LEVEL EXPECTATIONS **
***********************************

* Forecasts

gen forecast_whole = q10a

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=2 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 2


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecasts_district = q10

* Identify correct forecasts

gen correct_district_d = 0 if forecasts_district!=district_outcome & forecasts_district!=.
replace correct_district_d = 1 if forecasts_district == district_outcome


**********
** SAVE **
**********

save "on2011_ipsos.dta", replace


********************************
** MAIN ANALYSES (PROVINCIAL) **
********************************

* Regression analysis and percentage of correct and incorrect forecasts (provincial)

logistic correct_whole_d discussion i.vote_whole age i.male education
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


*************************************
** SUMMARY STATISTICS (PROVINCIAL) **
*************************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable vote_whole "Provincial vote"
label variable vote_district "District vote"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable enep "ENEP"
label variable margin "Margin"
label variable reelected "Reelected"

estpost tabstat correct_whole_d discussion vote_whole age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "on2011_ipsos_summary_provincial.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2011 Ontario provincial election (Ipsos), provincial level") replace

eststo clear


**********
** SAVE **
**********

save "on2011_ipsos_provincial_merge.dta", replace


******************************
** MAIN ANALYSES (DISTRICT) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion i.vote_district age i.male education enep i.reelected || district_code: , or
estimates table, star(.05 .01 .001)

parmest, format(estimate min95 max95 %8.2f p %8.1e) list(,) saving(on2011_ipsos_parmest_district_full.dta, replace)

tab correct_district_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (DISTRICT) **
***********************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable vote_whole "Provincial vote"
label variable vote_district "District vote"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable enep "ENEP"
label variable margin "Margin"
label variable reelected "Reelected"

estpost tabstat correct_district_d discussion vote_district age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "on2011_ipsos_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2011 Ontario provincial election (Ipsos), district level") replace

eststo clear

log close